package com.malbbmsomanager.caladapter;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DM_BBM_CusListener implements OnClickListener {
	
	private Activity activity;
	private Context context;
	
	private TextView display;
	
	private TextView mCalculatorDisplay;
    private Boolean userIsInTheMiddleOfTypingANumber = false;
    private DM_BBM_CalculatorBrain mCalculatorBrain;
    private static final String DIGITS = "0123456789.";
    
    DecimalFormat df = new DecimalFormat("@###########");

    public DM_BBM_CusListener(TextView passView,DM_BBM_CalculatorBrain calbr) {
    	this.display = passView;
    	this.mCalculatorBrain = calbr;
	}
    
    @Override
    public void onClick(View v) {
       
    	//view.findViewById(view.getId());
		
    			//view.getId();
    	
    	//mCalculatorBrain = new CalculatorBrain();
        mCalculatorDisplay = display;
        
        df.setMinimumFractionDigits(0);
        df.setMinimumIntegerDigits(1);
        df.setMaximumIntegerDigits(8);
    			
    			//String buttonPressed = ((Button) v.findViewById(v.getId())).getText().toString();
    			
    			 String buttonPressed = ((Button) v).getText().toString();
    			 
    			 Log.e("buttonPressed", buttonPressed);
    			 Log.e("button Text", display.getText().toString());
    			 
    			 Log.e("userIsInTheMiddleOfTypingANumber",""+userIsInTheMiddleOfTypingANumber);
    			 
    		        if (DIGITS.contains(buttonPressed)) {
    		 
    		            // digit was pressed
    		            if (userIsInTheMiddleOfTypingANumber) {
    		 
    		                if (buttonPressed.equals(".") && mCalculatorDisplay.getText().toString().contains(".")) {
    		                    // ERROR PREVENTION
    		                    // Eliminate entering multiple decimals
    		                } else {
    		                    mCalculatorDisplay.append(buttonPressed);
    		                }
    		 
    		            } else {
    		 
    		                if (buttonPressed.equals(".")) {
    		                    // ERROR PREVENTION
    		                    // This will avoid error if only the decimal is hit before an operator, by placing a leading zero
    		                    // before the decimal
    		                    mCalculatorDisplay.setText(0 + buttonPressed);
    		                } else {
    		                    mCalculatorDisplay.setText(buttonPressed);
    		                }
    		 
    		                userIsInTheMiddleOfTypingANumber = true;
    		            }
    		 
    		        } else {
    		            // operation was pressed
    		            if (userIsInTheMiddleOfTypingANumber) {
    		 
    		                mCalculatorBrain.setOperand(Double.parseDouble(mCalculatorDisplay.getText().toString()));
    		                userIsInTheMiddleOfTypingANumber = false;
    		            }
    		 
    		            mCalculatorBrain.performOperation(buttonPressed);
    		            mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
    		 
    		        }

    }

}
